var data1 = [{'id':'1','contractno':'合同1','customercontractno':'text2','customername':'text3',
    'sdate':'text4','stockupperiod':'text5','shdate':'text6','modetransportation':'text7'},
    {'id':'2','contractno':'合同2','customercontractno':'text2','customername':'text3',
        'sdate':'text4','stockupperiod':'text5','shdate':'text6','modetransportation':'text7'},
    {'id':'3','contractno':'合同3','customercontractno':'text2','customername':'text3',
        'sdate':'text4','stockupperiod':'text5','shdate':'text6','modetransportation':'text7'}];

var data2 = [{'id':'1','productcode':'产品1','specificationcoding':'text2','specification':'text3' } ];
var layer = layui.layer //弹层
    ,table = layui.table //表格
form = layui.form,$ = layui.jquery,changeList=[],laydate = layui.laydate,indexs = 0,checkboxs =false;
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});
table.render({
    elem: '#mains'
    //,data:data1
    ,url:"exportContract/import/getContractByPage"
    ,where: {
        "billNo":$("input[name='billNo']")[0].value,
        "customerContactNo":$("input[name='customerContactNo']")[0].value,
        "signingDateStart":$("input[name='signingDateStart']")[0].value,
        "signingDateEnd":$("input[name='signingDateEnd']")[0].value,
        "customerName":$("input[name='customerName']")[0].value,
        "isDeclare":$("select[name='isDeclare']")[0].value

    }
    ,method:"get"
    ,cols: [[
        {checkbox: true, sort : true,title:'选择'}
        ,{field:'billNo', title:'合同编号',sort:true}
        ,{field:'customerCode', title:'客户合同号',sort:true}
        ,{field:'customerName', title:'客户名称',sort:true}
        ,{field:'signingDate', title:'签约日期',sort:true}
        ,{field:'leadTime', title:'备货期限',sort:true}
        ,{field:'carryTime', title:'出运日期',sort:true}
        ,{field:'modeOfTransport', title:'运输方式',sort:true}
        ,{field:'salesmanCode', title:'业务员',sort:true}
        ,{field:'businessModelName', title:'业务模式',sort:true}
    ]]
    ,id: 'mainstableid'
    ,height: 300
    ,page: true
    ,done: function (res, curr, count) {
        table.render({
            elem: '#product'
            ,method:'get'
            ,url:'exportContract/import/getContractProductByPage'
            ,where: {
                id:res.data.length == 0 ? "":res.data[0].id //默认第一条信息，数据为空则id为空
            }
            //,data:data2
            ,cols: [[
                {checkbox: true, sort : true,title:'选择'}
                ,{field:'productCode', title:'产品编码', sort : true}
                ,{field:'specificationCode', title:'规格编码', sort : true}
                ,{field:'specificationExplanation', title:'规格说明', sort : true}
                ,{field: 'clientProductNo', title: '客户货号', sort: true}
                ,{field:'productCname', title:'中文名称', sort : true}
                ,{field:'productEname', title:'英文名称', sort : true}
                ,{field: 'productCdesc', title: '中文描述', sort: true}
                ,{field: 'productEdesc', title: '英文描述', sort: true}
                ,{field:'productNum', title:'数量', sort : true}
                ,{field:'productUnitCode', title:'单位', sort : true}
                ,{field:'productSurplusNum', title:'剩余数量', sort : true}
                ,{field: 'unitPrice', title: '单价', sort : true}
                ,{field: 'moneySubtotal', width: 120, title: '金额小计', sort : true}
            ]]
            ,id: 'producttableid'
            ,where: {
                id:res.data.length == 0 ? "":res.data[0].id //默认第一条信息，数据为空则id为空
            }
            ,height: 400
            ,done:function () {
                $("th .layui-table-cell.laytable-cell-checkbox").html("选择");
            }
        });
        if(res.data.length != 0){
            var jsons = {"id":res.data[0].id,"changeList":[]}
            changeList.push(jsons);
        }
    }
});

//监听复选框
table.on('checkbox(mainstable)', function (data) {
    checkboxs = true;
    table.reload('producttableid', {
        method:'get'
        ,url:'exportContract/import/getContractProductByPage'
        ,where: {
              id:data.data.id  //res.data.length == 0 ? "":res.data[0].id //默认第一条信息，数据为空则id为空
           }
        ,done: function (res, curr, count) {
            var shu = 0;
            for(var i = 0;i<changeList.length;i++){
                if(changeList[i].id == data.data.id){
                    indexs = i;
                    shu ++;
                }
            }
            if(shu == 0){
                var jsons = {"id":data.data.id,"changeList":[]};
                changeList.push(jsons);
                indexs = changeList.length-1;
            }
            var child = $("#product").next().find('tbody input[type="checkbox"]');
            child.each(function(index, item){
                item.checked = data.checked;
            });
            if(data.checked){　　　　　　//判断当前多选框是选中还是取消选中
                changeList[indexs].changeList = layui.table.cache["producttableid"];
            }else{
                changeList[indexs].changeList = [];
            }
            $("th .layui-table-cell.laytable-cell-checkbox").html("选择");
            form.render('checkbox');
            layer.closeAll();    //返回数据关闭loading
        }
    }, 'data');
});

//监听复选框
table.on('checkbox(producttable)', function (obj) {
    var index = layer.load(1); //添加laoding
    //如果checked为false
    if (!obj.checked) {
        if(obj.type=="all"){
            changeList[indexs].changeList = [];
        }else{
            for (var i = 0; i < changeList[indexs].changeList.length; i++) {
                //如果数据中有这个数据就移除
                if (changeList[indexs].changeList[i].id == obj.data.id) {
                    changeList[indexs].changeList.splice(i, 1);
                }
            }
        }
    }else{
        if(obj.type=="all"){
            changeList[indexs].changeList = layui.table.cache["producttableid"];
        }else{
            //如果没有这个数据就加进去
            changeList[indexs].changeList.splice(changeList[indexs].changeList.length, 0, obj.data);
        }
    };

    var child = $("#mains").next().find('tbody tr');
    //input[type="checkbox"]
    var mainsdata = layui.table.cache["mainstableid"];
    child.each(function(index, item){
        var dataindex =  $(item).attr("data-index");
        if(mainsdata[dataindex].id == changeList[indexs].id && $(item).find('td input[type="checkbox"]')[0].checked != true){
            $(item).find('td input[type="checkbox"]')[0].checked = true;
        }
    });
    // form.render('checkbox');task/import/getTaskProductByPage
    table.reload('producttableid', {
        method:'get'
        ,url:'exportContract/import/getContractProductByPage'
        ,where: {
            id:changeList[indexs].id //默认第一条信息，数据为空则id为空
        }
       // data:data1
        //,where: changeList[indexs].id//点击行ID
        ,done: function (res, curr, count) {
            var subscript = [];
            for(var i = 0 ;i<res.data.length;i++){
                for(var j = 0;j<changeList[indexs].changeList.length;j++){
                    if(res.data[i].id == changeList[indexs].changeList[j].id){
                        var k = i;
                        subscript.push(k);
                    }
                }
            }
            var child = $("#product").next().find('tbody input[type="checkbox"]');
            child.each(function(index, item){
                var shul = 0;
                for(var i = 0 ;i<subscript.length;i++){
                    if(index == subscript[i]){
                        shul++;
                    }
                }
                if(shul == 0){
                    item.checked = false;
                }else{
                    item.checked = true;
                }
            });
            $("th .layui-table-cell.laytable-cell-checkbox").html("选择");
            form.render('checkbox');
            layer.close(index);    //返回数据关闭loading
        }
    }, 'data');
});

//单击行事件
table.on('row(mainstable)', function (obj) {
    checkboxs = false;
    var index = layer.load(1); //添加laoding
    setTimeout(function (){
        if(checkboxs != true){
            table.reload('producttableid', {
                method:'get'
                ,url:'exportContract/import/getContractProductByPage'
                ,where: {
                     id:obj.data.id //默认第一条信息，数据为空则id为空
                 }
                //,where: obj.data.id //点击行ID
                ,done: function (res, curr, count) {
                    var shu = 0;
                    for(var i = 0;i<changeList.length;i++){
                        if(changeList[i].id == obj.data.id){
                            indexs = i;
                            shu ++;
                        }
                    }
                    if(shu != 0){
                        var subscript = [];
                        for(var i = 0 ;i<res.data.length;i++){
                            for(var j = 0;j<changeList[indexs].changeList.length;j++){
                                if(res.data[i].id == changeList[indexs].changeList[j].id){
                                    var k = i;
                                    subscript.push(k);
                                }
                            }
                        }
                        var child = $("#product").next().find('tbody input[type="checkbox"]');
                        child.each(function(index, item){
                            var shul = 0;
                            for(var i = 0 ;i<subscript.length;i++){
                                if(index == subscript[i]){
                                    shul++;
                                }
                            }
                            if(shul == 0){
                                item.checked = false;
                            }else{
                                item.checked = true;
                            }
                        });
                    }else{
                        var jsons = {"id":obj.data.id,"changeList":[]};
                        changeList.push(jsons);
                        indexs = changeList.length-1;
                        var child = $("#product").next().find('tbody input[type="checkbox"]');
                        child.each(function(index, item){
                            item.checked = false;
                        });
                    }
                    $("th .layui-table-cell.laytable-cell-checkbox").html("选择");
                    form.render('checkbox');
                    layer.close(index);    //返回数据关闭loading
                }
            }, 'data');
        }
    }, 500);
});


form.on('submit(submitform)', function(data){
   // layer.msg(JSON.stringify(data.field));
    table.reload('mainstableid', {
        url:"exportContract/import/getContractByPage"
        ,where: {
        "billNo":$("input[name='billNo']")[0].value,
            "customerContactNo":$("input[name='customerContactNo']")[0].value,
            "signingDateStart":$("input[name='signingDateStart']")[0].value,
            "signingDateEnd":$("input[name='signingDateEnd']")[0].value,
            "customerName":$("input[name='customerName']")[0].value,
            "isDeclare":$("select[name='isDeclare']")[0].value

        }
        ,method:"get"
        ,page: {
            curr: 1 //重新从第 1 页开始
        }

    }, 'data');
    return false;
});

$("#choose").on("click",function () {
    var purchaseorderdata =   table.checkStatus("mainstableid").data; //第一个表格选中的数据
    parent.wxandbhflag2("w");
    parent.cyjhreshWX(purchaseorderdata,changeList);//将两个表格中选择的数据传递给父对象
    setTimeout(function () {
        //保存成功则关闭此页面
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    },1000);
    /*if(purchaseorderdata.length + changeList.length == 0){
        layer.msg("请选择数据")
    }else{
        //执行接口
        layer.msg("保存成功", {icon: 6});

    }*/
})
$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})